package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页条件查询套餐
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        //添加套餐
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        //获取添加套餐的主键
        Long setmealId = setmeal.getId();

        //添加套餐包含的菜品
        //为每个菜品设置套餐id
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //批量添加套餐菜品
        setmealDishMapper.insertSetmeals(setmealDishes);
    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断套餐是否为起售状态,
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == 1) {
                //如果为起售状态不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //批量删除套餐
        setmealMapper.deleteByIds(ids);
        //批量删除套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询套餐菜品关系中间表
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);

        //将套餐菜品关系中间表数据,设置到SetmealVO中
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        System.out.println("setmeal:=========================>" + setmeal);
        setmealMapper.update(setmeal);

        //修改套餐菜品关系表
        //删除原始套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //setmeal:=========================>[SetmealDish(id=null, setmealId=null, dishId=82, name=麻辣鸡头, price=50, copies=3),
        System.out.println("setmealDishes(获取前端套餐菜品关系):=========================>" + setmealDishes);
        //删除改套餐下的菜品
        Long setmealId = setmeal.getId();//套餐id
        List<Long> setmealIds = Arrays.asList(setmealId);
        setmealDishMapper.deleteBySetmealIds(setmealIds);

        //添加新的套餐菜品关系
        //为每一个新的套餐菜品关系设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        System.out.println("setmealDishes(为新的套餐菜品关系设置套餐id):=========================>" + setmealDishes);
        setmealDishMapper.insertSetmeals(setmealDishes);

    }

    /**
     * 停售起售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //修改套餐状态
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(setmeal);

        //起售套餐时，判断套餐内是否包含停售的菜品，如果包含停售菜品不能删除
        if (status == StatusConstant.ENABLE) {
            //查询套餐内的菜品是否为起售状态,如果为起售状态则抛出异常
            //查询套餐菜品关系表信息
            List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);
            //判断套餐菜品关系表信息中,是否有停售的菜品
            for (SetmealDish setmealDish : setmealDishes) {
                //套餐中的菜品id
                Long dishId = setmealDish.getDishId();
                //查询菜品是否为停售状态
                Dish dish = dishMapper.selectById(dishId);
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    //如果为起售状态则抛出异常
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
